package com.a3dweijing.wetouch2.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.Util.GlideUtil;
import com.a3dweijing.wetouch2.bean.PartitionTypeRecomend;
import com.a3dweijing.wetouch2.callback.PartitionOnclick;
import com.a3dweijing.wetouch2.recyclerviewdecoration.RecycleViewDivider;

import java.util.List;

/**
 * Created by yxd on 2017/11/3.
 */

public class PartitionRecommendRecAdapter extends RecyclerView.Adapter<PartitionRecommendRecAdapter.MyViewHolder> {
    private List<PartitionTypeRecomend> recomends;
    private Context context;
    private PartitionOnclick onclick;

    public PartitionRecommendRecAdapter(List<PartitionTypeRecomend> recomends, Context context, PartitionOnclick onclick) {
        this.recomends = cleanNull(recomends);
        this.context = context;
        this.onclick=onclick;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_partition_bottom,parent,false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.type_name.setText(recomends.get(position).getWc_type_name());
        holder.type_rec.setLayoutManager(new GridLayoutManager(context,2));
        holder.type_rec.addItemDecoration(new RecycleViewDivider(10, Color.WHITE));
        holder.type_rec.setAdapter(new PartitionRecItemAdapter(recomends.get(position).getOriginalVideoList(),onclick));
        holder.type_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onclick!=null){
                    onclick.typeclick(position,recomends.get(position));
                }
            }
        });
//        ImageLoaderUtil.getLoader().loadImg4(recomends.get(position).getWc_type_img(),holder.type_pic);
        GlideUtil.getInstance().showImage(context,"https://lfriend.oss-cn-shenzhen.aliyuncs.com/"+recomends.get(position).getOriginalVideoList().get(0).getWc_type_img(),holder.type_pic);
    }

    @Override
    public int getItemCount() {
        return recomends.size();
    }

    private List<PartitionTypeRecomend> cleanNull(List<PartitionTypeRecomend> recomend){
        int count=recomend.size()-1;
        for (int i=count;i>=0;i--){
            if (recomend.get(i).getOriginalVideoList()!=null && recomend.get(i).getOriginalVideoList().size()>0){

            }else {
                recomend.remove(i);
            }
        }
        return recomend;
    }


    class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView type_pic;
        TextView type_name;
        TextView type_more;
        RecyclerView type_rec;
        public MyViewHolder(View itemView) {
            super(itemView);
            type_pic= (ImageView) itemView.findViewById(R.id.video_type_pic);
            type_name= (TextView) itemView.findViewById(R.id.video_type_name);
            type_more= (TextView) itemView.findViewById(R.id.video_type_more);
            type_rec= (RecyclerView) itemView.findViewById(R.id.video_type_recomment);
        }
    }

}
